Download Image Using AsyncTask In Android
In this tutorial, you will learn how to download an image from a URL address into your Android application. We...
In this tutorial, you will learn how to download an image from a URL address into your Android application. We will create a button and on button click will start an AsyncTask class to begin downloading an image from a URL address. So lets begin…
Create a new project in Eclipse File > New > Android Application Project. Fill in the details and name your project DownloadImageTutorial.
Application Name : DownloadImageTutorial
Project Name : DownloadImageTutorial
Package Name : com.androidbegin.downloadimagetutorial
Open your MainActivity.java and paste the following code.
MainActivity.java
In this activity, we have created a Button and an ImageView and on button click will show a progress dialog and start the Download Image AsyncTask class. The download image will be decoded into a bitmap and set into an ImageView. In this tutorial, we’ve hosted the sample image GIF file in our server. Click on this link to see the sample image. http://www.androidbegin.com/wp-content/uploads/2013/07/HD-Logo.gif
Next, create an XML file for your MainActivity graphical layout. Go to res > layout > Right Click on layout > New > Android XML File
Name your new XML file activity_main.xml and paste the following code.
activity_main.xml
Next, change the application name and texts. Open your strings.xml in your res > values folder and paste the following code.
strings.xml
strings.xml
In your AndroidManifest.xml, we need to declare a permission to connect to the Internet. Open your AndroidManifest.xml and paste the following code.
AndroidManifest.xml
Output :
Source Code